package dk.ahp.osmmaptest;

import android.os.Bundle;
import android.app.Activity;

import java.util.ArrayList;
import org.osmdroid.DefaultResourceProxyImpl;
import org.osmdroid.ResourceProxy;
import org.osmdroid.api.IGeoPoint;
import org.osmdroid.tileprovider.tilesource.TileSourceFactory;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapController;
import org.osmdroid.views.MapView;
import org.osmdroid.views.MapView.Projection;
import org.osmdroid.views.overlay.ItemizedIconOverlay;
import org.osmdroid.views.overlay.ItemizedOverlay;
import org.osmdroid.views.overlay.MyLocationOverlay;
import org.osmdroid.views.overlay.OverlayItem;
import org.osmdroid.views.util.constants.MapViewConstants;

import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

public class DemoMap extends Activity {

    private MapView mMapView;
    private MapController mapController;
//    private ItemizedOverlay<OverlayItem> mMyLocationOverlay;
    private ResourceProxy mResourceProxy;
    private MyLocationOverlay myLocationOverLay;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mResourceProxy = new DefaultResourceProxyImpl(getApplicationContext());
        setContentView(R.layout.activity_demo_map);

        mMapView = (MapView) this.findViewById(R.id.mapview);
        mMapView.setTileSource(TileSourceFactory.MAPNIK);
        mMapView.setBuiltInZoomControls(true);
        mMapView.setMultiTouchControls(true);
        mapController = this.mMapView.getController();
        mapController.setZoom(15);
        GeoPoint point2 = new GeoPoint(53554070, -2959520); // centre map here
        GeoPoint point3 = new GeoPoint(53554070 + 1000, -2959520 + 1000); // icon goes here
        mapController.setCenter(point2);

//        ArrayList<OverlayItem> items = new ArrayList<OverlayItem>();
//        // Put overlay icon a little way from map centre
//        items.add(new OverlayItem("Here", "SampleDescription", point3));
//
//        /* OnTapListener for the Markers, shows a simple Toast. */
//        this.mMyLocationOverlay = new ItemizedIconOverlay<OverlayItem>(items,
//                new ItemizedIconOverlay.OnItemGestureListener<OverlayItem>() {
//                    @Override
//                    public boolean onItemSingleTapUp(final int index, final OverlayItem item) {
//                        Toast.makeText( DemoMap.this, "Item '" + item.mTitle, Toast.LENGTH_LONG).show();
//                        return true; // We 'handled' this event.
//                    }
//                    @Override
//                    public boolean onItemLongPress(final int index, final OverlayItem item) {
//                        Toast.makeText( DemoMap.this, "Item Long'" + item.mTitle ,Toast.LENGTH_LONG).show();
//                        return false;
//                    }
//                    
//                }, mResourceProxy);
//        this.mMapView.getOverlays().add(this.mMyLocationOverlay);
        myLocationOverLay = new MyLocationOverlay(this, mMapView);
        this.mMapView.getOverlays().add(myLocationOverLay);
        mMapView.invalidate();
        //mMapView.setOnClickListener(this);
    }

//	@Override
//	public void onClick(View arg0) {
//		//myLocationOverLay.g
//		Toast t = Toast.makeText(DemoMap.this, "Noget text:\n" + arg0.toString(), Toast.LENGTH_LONG);
//		t.show();
//	}
    
	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
	    int actionType = ev.getAction();
	    switch (actionType) {
	    case MotionEvent.ACTION_UP:
	            Projection proj = mMapView.getProjection();
	            IGeoPoint loc = proj.fromPixels((int)ev.getX(), (int)ev.getY());
//	            GeoPoint loc = proj.fromPixels((int)ev.getX(), (int)ev.getY()); 
	            String longitude = Double.toString(((double)loc.getLongitudeE6())/1000000);
	            String latitude = Double.toString(((double)loc.getLatitudeE6())/1000000);
//
	             Toast toast = Toast.makeText(getApplicationContext(), "Longitude: "+ longitude +" Latitude: "+ latitude , Toast.LENGTH_LONG);
	            toast.show();

	    }

	    return super.dispatchTouchEvent(ev);
	}
}